<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 服务栏目
 *
 * @icon fa fa-circle-o
 */
class Nav extends Backend
{
    protected $noNeedRight = ['category'];

    /**
     * Nav模型对象
     * @var \app\admin\model\Nav
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Nav;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("showIndexList", $this->model->getShowIndexList());

        $ruleList = collection($this->model->order('weigh', 'desc')->select())->toArray();
        Tree::instance()->init($ruleList)->icon = ['&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;'];
        $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        $ruledata = [0 => __('None')];
        foreach ($this->rulelist as $k => &$v) {
            $ruledata[$v['id']] = $v['name'];
        }
        $this->view->assign('ruledata', $ruledata);
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = $this->rulelist;
            $total = count($this->rulelist);

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post()['row'];
            //判断当前分类级别
            $topCount = $this->getTopCount($data['pid']);
            if ($topCount > 1) $this->error('最多添加二级分类');
            $this->model->create($data);
            $this->success('添加成功');
        }
        return $this->fetch();
    }

    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post()['row'];
            $info = $this->model->find($data['id']);
            if ($info['id'] == $data['pid']) $this->error('不能修改自己为自己的上级');
            //判断当前分类级别
            $topCount = $this->getTopCount($data['pid']);
            if ($this->model->where('pid', $info['id'])->find()) {
                $topCount++;
            }
            if ($topCount > 1) $this->error('最多添加二级分类');

            $info->save($data);
            $this->success('操作成功');
        }
        $row = $this->model->get($ids);
        $this->assign('row', $row);
        return $this->fetch();
    }

    public function getTopCount($pid, $count = 0)
    {
        static $count;
        if ($pid) {
            $count++;
            $category = $this->model->find($pid);
            $this->getTopCount($category['pid'], $count);
        }
        return $count;
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $delIds = [];
            foreach (explode(',', $ids) as $k => $v) {
                $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
            }
            $delIds = array_unique($delIds);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count) {
                $this->success();
            }
        }
        $this->error();
    }




    /**
     * 读取分类数据,联动列表
     */
    public function category()
    {
        $pid = $this->request->get('pid', 0);
        $where['pid'] = $pid;
        $lists = Db::name('nav')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
        $this->success('', '', $lists);
    }
}
